package com.well.kernel.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Copyright &copy; Well All rights reserved.
 * Author:Well
 * Date:2016/12/16
 * Description:编写测试用例时，获取注入在spring中的bean
 */

public class SpringUtil 
{
	/* spring配置文件 */
	private static final String SPRING_CONFIG = "spring/spring.xml";
	
	/* 获取spring的上下文 */
	private static ApplicationContext applicationContext = new ClassPathXmlApplicationContext(SPRING_CONFIG);

	/**
	 * 获取对应类型的bean
	 * @param clazz 类型
	 * @return bean
	 */
	public static <T> T getBean(Class<T> clazz)
	{
		return applicationContext.getBean(clazz);
	}

	/**
	 * 获取spring中指定bean名称的bean
	 * @param name bean名称
	 * @return bean
	 */
	public static <T>T getBean(String name)
	{
		return (T)applicationContext.getBean(name);
	}
}
